﻿(function ($) {
    var _ajax = $.ajax;
    $.ajax = function (opt) {

        var token = $.cookie('token');

        var fn = {
            error: function (XMLHttpRequest, textStatus, errorThrown) { },
            success: function (data, textStatus) { }
        }
        if (opt.error) {
            fn.error = opt.error;
        }
        if (opt.success) {
            fn.success = opt.success;
        }
        //扩展增强处理     
        var _opt = $.extend(opt, {
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                fn.error(XMLHttpRequest, textStatus, errorThrown);
            },
            headers: {
                'token': token
            },
            success: function (data, textStatus) {
                if (data && data.IsLogout) {
                    eval(data.Callback);
                }
                if (data && data.errorCode == 100) {
                    eval(data.Callback);
                }
                fn.success(data, textStatus);
            }
        });
        _ajax(_opt);
    };

    $.vailCenter = {
        //验证座机号码
        isTel: function (value) {
            var validateReg = /^0\d{2,3}-?\d{7,8}$/;
            return validateReg.test(value.trim());
        },
        //验证手机号码
        isMobile: function (value) {
            var validateReg = /^1[3|4|5|6|7|8]\d{9}$/;
            return validateReg.test(value.trim());
        },
        //验证邮箱格式
        isEmail: function (value) {
            var validateReg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
            return validateReg.test(value);
        },
        //验证是否为空
        isNullOrEmptySpance: function (value) {
            if (Object.prototype.toString.call(value) === "[object String]") {
                if ((value == null || value.trim() == "" || value == 'undefined' || value == 'null'))
                    return true;
            } else {
                if ((value == null || value == "" || value == 'undefined' || value == 'null'))
                    return true;
            }
            return false;
        },
        /**验证为正整数*/
        isPositiveInteger: function (value) {
            var validateReg = /^[0-9]*$/;
            return validateReg.test(value.trim());
        },
        isDate: function (value) {
            var validateReg = /^(\d+)-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):$/;;
            return validateReg.test(value.trim());
        },
        //身份证验证
        isIdCardNo: function (num) {
            num = num.toUpperCase();
            var factorArr = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1);
            var parityBit = new Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2");
            var varArray = new Array();
            var intValue;
            var lngProduct = 0;
            var intCheckDigit;
            var intStrLen = num.length;
            var idNumber = num;
            // initialize
            if ((intStrLen != 15) && (intStrLen != 18)) {
                return false;
            }
            // check and set value
            for (var i = 0; i < intStrLen; i++) {
                varArray[i] = idNumber.charAt(i);
                if ((varArray[i] < '0' || varArray[i] > '9') && (i != 17)) {
                    return false;
                } else if (i < 17) {
                    varArray[i] = varArray[i] * factorArr[i];
                }
            }
            if (intStrLen == 18) {
                //check date
                var date8 = idNumber.substring(6, 14);
                if (isDate8(date8) == false) {
                    return false;
                }
                // calculate the sum of the products
                for (i = 0; i < 17; i++) {
                    lngProduct = lngProduct + varArray[i];
                }
                // calculate the check digit
                intCheckDigit = parityBit[lngProduct % 11];
                // check last digit
                if (varArray[17] != intCheckDigit) {
                    return false;
                }
            } else { //length is 15
                //check date
                var date6 = idNumber.substring(6, 12);
                if (isDate6(date6) == false) {
                    return false;
                }
            }
            return true;
        }
    };
})(jQuery);

(function ($, window, undefined) {

    //全局命名空间
    var FlashPay = window.FlashPay || {};

    //设定基本框架
    FlashPay = {
        Base: {}, //基础层,所有的基础函数库,如数据验证、转换等
        DAO: {},  //数据访问层,取数据,一般为Ajax的套接口
        Util: {}, //前端工具层
        UI: {},   //前端工具层

    };
    //数据访问
    FlashPay.DAO = {};
    //工具
    FlashPay.Util = {};
    //前端展示函数库
    FlashPay.UI = {};

    //将EC导入到全局对象  
    window.FlashPay = FlashPay;

}($, window));